package com.lzx.controller;

import com.lzx.model.User;
import com.lzx.model.nybk.Nybk;
import com.lzx.service.nybk.NybkService;
import com.lzx.utils.SessionUtil;
import com.lzx.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/sy/nybk")
public class nybkController {
    @Autowired
    NybkService nybkService;

    @RequestMapping("/nybk.html")
    public ModelAndView nybk(HttpServletRequest request) throws SQLException {
        ModelAndView mv = new ModelAndView("system/nybk");
        int njtg = 0;
        int yfzs = 0;
        int zwbk = 0;
        int chfz = 0;
        List<Nybk> nybkByTypeNopage = nybkService.findNybkByTypeNopage(new Nybk());
        for (int i = 0; i < nybkByTypeNopage.size(); i++) {
            Nybk nybk1 = nybkByTypeNopage.get(i);
            if (nybk1.getType().equals("农技推广")) {
                njtg = njtg + 1;
            }
            if (nybk1.getType().equals("药肥知识")) {
                yfzs = yfzs + 1;
            }
            if (nybk1.getType().equals("虫害防治")) {
                chfz = chfz + 1;
            }
            if (nybk1.getType().equals("作物百科")) {
                zwbk = zwbk + 1;
            }
        }
        mv.addObject("njtg", njtg);
        mv.addObject("yfzs", yfzs);
        mv.addObject("zwbk", zwbk);
        mv.addObject("chfz", chfz);

        if(SessionUtil.getUser(request).getIsAdmin().equals("Y")){
            mv.addObject("isAdmn","TRUE");
        }else{
            mv.addObject("isAdmn","FALSE");
        }
        return mv;
    }


    @RequestMapping("/nybk_liebiao.html")
    public ModelAndView nybk_liebiao(HttpServletRequest request) throws SQLException {
        String type = request.getParameter("type");
        String type1 = "";
        if (type.equals("njtg")) {
            type1 = "农技推广";
        }
        if (type.equals("yfzs")) {
            type1 = "药肥知识";
        }
        if (type.equals("chfz")) {
            type1 = "虫害防治";
        }
        if (type.equals("zwbk")) {
            type1 = "作物百科";
        }


        String title = request.getParameter("title");
        String current = request.getParameter("current");
        if (StringUtils.isEmpty(current)) {
            current = "1";
        }
        Nybk nybk = new Nybk();
        nybk.setTitle(title);

        nybk.setType(type1);

        nybk.setFrom(String.valueOf((Integer.parseInt(current) - 1) * 10));
        ModelAndView mv = new ModelAndView("system/nybk_liebiao");
        List<Nybk> nybkAll = nybkService.findNybkByType(nybk);
        List<Nybk> nybkByTypeNopage = nybkService.findNybkByTypeNopage(nybk);
        int size = nybkByTypeNopage.size();
        if (size % 10 == 0) {
            mv.addObject("titleNum", size / 10);
        } else {
            mv.addObject("titleNum", size / 10 + 1);
        }

        mv.addObject("nybkAll", nybkAll);
        mv.addObject("type", type1);
        mv.addObject("title", title);
        mv.addObject("current", current);
        return mv;
    }
    @RequestMapping("/nybknew.html")
    public ModelAndView nybknew(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        ModelAndView mv = new ModelAndView("zhny/baiKe");
        String type=" ";
        type="农技推广";
        List njtg=nybkService.findNybktype(type);
        type="药肥知识";
        List yfzs=nybkService.findNybktype(type);
        type="作物百科";
        List zubk=nybkService.findNybktype(type);
        type="虫害防治";
        List chfz=nybkService.findNybktype(type);
        mv.addObject("njtg",njtg);
        mv.addObject("yfzs",yfzs);
        mv.addObject("zubk",zubk);
        mv.addObject("chfz",chfz);
        String locale = request.getParameter("locale");
        writeCookie(request,response,"locale",locale);
        User user = SessionUtil.getUser(request);
        mv.addObject("user", user);
        return mv;
    }

@RequestMapping(value = "/nybkDetil.html", method = RequestMethod.GET)
public ModelAndView nybkDetil(String id,HttpServletRequest request,HttpServletResponse response) throws SQLException {
    Nybk nybkById = nybkService.findNybkById(id);
    ModelAndView mv = new ModelAndView("zhny/baiKe_xx");
    String locale = request.getParameter("locale");
//        System.out.println("index========"+locale);
    writeCookie(request,response,"locale",locale);
    User user = SessionUtil.getUser(request);
    mv.addObject("user", user);
    mv.addObject("nybkById", nybkById);
    return mv;
}
    public static void writeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String value) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }
}
